package com.wholesale;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.view.GestureDetector;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;

import com.wholesale.beans.CartInfo;
import com.wholesale.beans.CollectGoods;
import com.wholesale.beans.GoodsDetail;
import com.wholesale.beans.GoodsGallery;
import com.wholesale.beans.GoodsInfo;
import com.wholesale.beans.WhoGoodsAttr;
import com.wholesale.commons.PageControlView;
import com.wholesale.datas.DbHelperAdapter;
import com.wholesale.datas.ShopCar;
import com.wholesale.datas.WebServiceClient;
import com.wholesale.utils.AsyncImageLoader;
import com.wholesale.utils.Constants;

public class GoodsDetailLayout extends LinearLayout implements OnClickListener {
	private ViewFlipper viewFlipper;
	private Context context;
	private View baseView;
	private Button leftButton;
	private Button rightButton;
	private TextView headView;

	private TableLayout selectTable;
	private Map<Integer, ArrayList<WhoGoodsAttr>> selectMap;

	private Map<Integer, WhoGoodsAttr> selectedMap;

	private Button addCartButton;
	private Button addFavoriteButton;
	private LinearLayout goodsDescLayout;
	private LinearLayout goodsCommentsLayout;
	private LinearLayout customSizeLayout;
	private LinearLayout customSizeLayoutMen;
	private GoodsDetail goodsDetail;
	private GoodsInfo goodsInfo;
	private GoodsGallery goodsGallery;

	private RelativeLayout galleryLayout;
	private RelativeLayout loadingLayout;
	private LinearLayout scrollView;
	// private MyViewGroup galleryView;
	private Gallery galleryView;
	private PageControlView pageControl;
	private GestureDetector myGesture;
	private ArrayList<Bitmap> bitmaps;

	private ArrayList<WhoGoodsAttr> whoGoodsAttrList;
	private ListView radioListView;
	private ProgressBar progress;

	private String goodsAttr;
	private AsyncImageLoader loader;

	private ImageAdapter imageAdapter;

	private boolean isFormal = false;

	// 构造函数
	public GoodsDetailLayout(Context context, final ViewFlipper viewFlipper,
			String backText, GoodsInfo goodsInfo) {
		super(context);
		this.viewFlipper = viewFlipper;
		this.context = context;
		this.goodsInfo = goodsInfo;
		// 加载需要的属性，加载方法一的子Layout
		baseView = ((Activity) getContext()).getLayoutInflater().inflate(
				R.layout.goodsdetail, this);
		leftButton = (Button) baseView.findViewById(R.id.leftBtn);
		leftButton.setText(backText);
		leftButton.setOnClickListener(this);
		rightButton = (Button) baseView.findViewById(R.id.rightBtn);
		rightButton.setVisibility(View.INVISIBLE);
		rightButton.setOnClickListener(this);
		headView = (TextView) baseView.findViewById(R.id.title);
		headView.setText("Detail");
		loadingLayout = (RelativeLayout) baseView
				.findViewById(R.id.loadingLayout);
		scrollView = (LinearLayout) baseView.findViewById(R.id.detailLayout);
		scrollView.setVisibility(View.GONE);

		selectTable = (TableLayout) baseView.findViewById(R.id.selectTable);
		addCartButton = (Button) baseView.findViewById(R.id.addCart);
		addCartButton.setOnClickListener(this);
		addFavoriteButton = (Button) baseView.findViewById(R.id.addFavorites);
		addFavoriteButton.setOnClickListener(this);
		goodsDescLayout = (LinearLayout) baseView
				.findViewById(R.id.goodsDescLayout);
		goodsDescLayout.setOnClickListener(this);
		goodsCommentsLayout = (LinearLayout) baseView
				.findViewById(R.id.goodsCommentLayout);
		goodsCommentsLayout.setOnClickListener(this);
		customSizeLayout = (LinearLayout) baseView
				.findViewById(R.id.customSizeLayout);
		customSizeLayoutMen = (LinearLayout) baseView
				.findViewById(R.id.customSizeLayoutMen);
		GoodsDetailAsync goodsDetailAsync = new GoodsDetailAsync(context);
		goodsDetailAsync.execute(goodsInfo.getGoodsId());
		initGrally();
	}

	public GoodsDetailLayout(Context context, final ViewFlipper viewFlipper,
			String backText, int goodsId) {
		super(context);
		this.viewFlipper = viewFlipper;
		this.context = context;
		this.goodsInfo = new GoodsInfo();
		this.goodsInfo.setGoodsId(goodsId);
		// 加载需要的属性，加载方法一的子Layout
		baseView = ((Activity) getContext()).getLayoutInflater().inflate(
				R.layout.goodsdetail, this);
		leftButton = (Button) baseView.findViewById(R.id.leftBtn);
		leftButton.setText(backText);
		leftButton.setOnClickListener(this);
		rightButton = (Button) baseView.findViewById(R.id.rightBtn);
		rightButton.setVisibility(View.INVISIBLE);
		rightButton.setOnClickListener(this);
		headView = (TextView) baseView.findViewById(R.id.title);
		headView.setText("Detail");
		loadingLayout = (RelativeLayout) baseView
				.findViewById(R.id.loadingLayout);
		scrollView = (LinearLayout) baseView.findViewById(R.id.detailLayout);
		scrollView.setVisibility(View.GONE);

		selectTable = (TableLayout) baseView.findViewById(R.id.selectTable);
		addCartButton = (Button) baseView.findViewById(R.id.addCart);
		addCartButton.setOnClickListener(this);
		addFavoriteButton = (Button) baseView.findViewById(R.id.addFavorites);
		addFavoriteButton.setOnClickListener(this);
		goodsDescLayout = (LinearLayout) baseView
				.findViewById(R.id.goodsDescLayout);
		goodsDescLayout.setOnClickListener(this);
		goodsCommentsLayout = (LinearLayout) baseView
				.findViewById(R.id.goodsCommentLayout);
		goodsCommentsLayout.setOnClickListener(this);
		customSizeLayout = (LinearLayout) baseView
				.findViewById(R.id.customSizeLayout);
		customSizeLayoutMen = (LinearLayout) baseView
				.findViewById(R.id.customSizeLayoutMen);
		GoodsDetailAsync goodsDetailAsync = new GoodsDetailAsync(context);
		goodsDetailAsync.execute(goodsInfo.getGoodsId());
		initGrally();
	}

	private void initGrally() {
		galleryLayout = (RelativeLayout) baseView
				.findViewById(R.id.galleryLayout);
		progress = (ProgressBar) baseView.findViewById(R.id.progress);
		// 位图资源 不要小于2张
		bitmaps = new ArrayList<Bitmap>();
		// galleryView = (MyViewGroup) baseView.findViewById(R.id.myGrally);
		// galleryView.setOnClickCallback(new OnClickCallback() {
		// @Override
		// public void callback(int currentIndex) {
		// if (goodsGallery != null) {
		// ShowPhotoLayout showPhotoLayout = new ShowPhotoLayout(
		// context, viewFlipper, goodsGallery, currentIndex);
		// viewFlipper.addView(showPhotoLayout);
		// viewFlipper.showNext();
		// }
		// }
		// });
		galleryView = (Gallery) baseView.findViewById(R.id.myGrally);
		galleryView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				ShowPhotoLayout showPhotoLayout = new ShowPhotoLayout(context,
						viewFlipper, goodsGallery, arg2);
				viewFlipper.addView(showPhotoLayout);
				viewFlipper.showNext();
			}

		});
	}

	public void setGrallyPostion(int position) {
		// galleryView.scrollToScreen(position);
		// galleryView.s
		// flingView.setPostion(position);
	}

	class GoodsDetailAsync extends AsyncTask<Integer, Integer, GoodsDetail> {
		ProgressDialog pdialog;

		public GoodsDetailAsync(Context context) {
		}

		@Override
		protected void onPreExecute() {
			loadingLayout.setVisibility(View.VISIBLE);
		}

		@Override
		protected GoodsDetail doInBackground(Integer... params) {
			// 添加历史记录
			if (goodsInfo.getGoodsName() != null) {
				DbHelperAdapter dbHelperAdapter = new DbHelperAdapter(context);
				dbHelperAdapter.open();
				dbHelperAdapter.createHistory(goodsInfo);
				dbHelperAdapter.close();
			}

			if (Constants.whoAttributeList == null) {
				Constants.whoAttributeList = WebServiceClient.getAttributes();
			}
			whoGoodsAttrList = WebServiceClient.getGoodsAttributes(params[0]);
			selectMap = new HashMap<Integer, ArrayList<WhoGoodsAttr>>();
			selectedMap = new HashMap<Integer, WhoGoodsAttr>();
			for (WhoGoodsAttr whoGoodsAttr : whoGoodsAttrList) {
				if (selectMap.get(whoGoodsAttr.getAttrId()) != null) {
					selectMap.get(whoGoodsAttr.getAttrId()).add(whoGoodsAttr);
				} else {
					String whoAttributeName = Constants
							.getWhoAttributeName(whoGoodsAttr.getAttrId());
					if (null == whoAttributeName || whoAttributeName.equals("")
							|| whoAttributeName.equalsIgnoreCase("null")) {
						continue;
					}
					// selectTable.setVisibility(View.VISIBLE);
					ArrayList<WhoGoodsAttr> list = new ArrayList<WhoGoodsAttr>();
					list.add(whoGoodsAttr);
					selectMap.put(whoGoodsAttr.getAttrId(), list);
					selectedMap.put(whoGoodsAttr.getAttrId(), whoGoodsAttr);
					TableRow tableRow = new TableRow(context);
					tableRow.setBackgroundResource(R.drawable.goods_detail_cell_small);
					tableRow.setGravity(Gravity.CENTER);
					TextView textView = new TextView(context);
					textView.setTextColor(Color.BLACK);
					textView.setTextSize(14);
					// textView.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
					textView.setText(whoAttributeName + " :  ");
					textView.setLayoutParams(new TableRow.LayoutParams(
							TableRow.LayoutParams.FILL_PARENT,
							TableRow.LayoutParams.WRAP_CONTENT));
					textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT);
					tableRow.addView(textView);
					Button button = new Button(context);
					button.setPadding(0, 0, 10, 0);
					button.setTag(whoGoodsAttr.getAttrId());
					button.setBackgroundResource(R.drawable.colorsize_btn);
					button.setTextSize(14);
					button.setTextColor(Color.WHITE);
					// button.setPadding(5, 0, 15, 0);
					// button.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
					String value = whoGoodsAttr.getAttrValue();
					button.setText(value);
					button.setLayoutParams(new TableRow.LayoutParams(
							TableRow.LayoutParams.WRAP_CONTENT,
							TableRow.LayoutParams.WRAP_CONTENT));
					button.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);

					if (value.indexOf("size") != -1) {
						button.setOnClickListener(new SelectClickListener(
								context, true));
					} else {
						button.setOnClickListener(new SelectClickListener(
								context, false));
					}
					tableRow.addView(button);

					if (value.indexOf("custom size") != -1
							|| value.indexOf("Custom size") != -1
							|| value.indexOf("custom Size") != -1
							|| value.indexOf("Custom Size") != -1) {
						isFormal = true;
					}

					selectTable.addView(tableRow);
				}
			}
			if (Constants.loginResponse != null) {
				return WebServiceClient.getGoodsDetail(params[0],
						Constants.loginResponse.getUserId(),
						Constants.loginResponse.getUserToken());
			} else {
				return WebServiceClient.getGoodsDetail(params[0]);
			}
		}

		@Override
		protected void onPostExecute(GoodsDetail result) {
			if (result != null) {
				goodsDetail = result;
				((TextView) baseView.findViewById(R.id.goodsName))
						.setText(goodsDetail.getGoodsName());
				((TextView) baseView.findViewById(R.id.goodsSN_text))
						.setText(goodsDetail.getGoodsSn());
				((TextView) baseView.findViewById(R.id.goodsWeight_text))
						.setText("" + goodsDetail.getGoodsWeight());
				((TextView) baseView.findViewById(R.id.goodsPrice_text))
						.setText("$ " + goodsDetail.getShopPrice());
				if (goodsDetail.getDiscountPrice() > 0) {
					// 显示折扣价格
					((TextView) baseView.findViewById(R.id.userPrice_lable))
							.setText(goodsDetail.getDiscountName() + " :");
					((TextView) baseView.findViewById(R.id.userPrice_text))
							.setText("$ " + goodsDetail.getDiscountPrice());
				} else {
					((TextView) baseView.findViewById(R.id.userPrice_lable))
							.setVisibility(View.GONE);
					((TextView) baseView.findViewById(R.id.userPrice_text))
							.setVisibility(View.GONE);
				}
				if (isFormal) {
					if (goodsDetail.getCatId() == Constants.menFormalId) {
						customSizeLayoutMen.setVisibility(View.VISIBLE);
					} else {
						customSizeLayout.setVisibility(View.VISIBLE);
					}
				}
			}
			// pdialog.dismiss();
			loadingLayout.setVisibility(View.GONE);
			scrollView.setVisibility(View.VISIBLE);
			GoodsGalleryAsync goodsGalleryAsync = new GoodsGalleryAsync(context);
			goodsGalleryAsync.execute(goodsInfo.getGoodsId());
		}

		@Override
		protected void onCancelled() {
			super.onCancelled();
		}
	}

	class AddGoodsAsync extends AsyncTask<Integer, Integer, Boolean> {
		ProgressDialog pdialog;
		int type;

		public AddGoodsAsync(Context context) {
		}

		@Override
		protected void onPreExecute() {
			loadingLayout.setVisibility(View.VISIBLE);
		}

		@Override
		protected Boolean doInBackground(Integer... params) {
			type = params[0];
			if (type == 1) { // 添加收藏
				CollectGoods collectGoods = new CollectGoods();
				collectGoods.setUserId(Constants.loginResponse.getUserId());
				collectGoods.setGoodsId(goodsInfo.getGoodsId());
				collectGoods.setGoodsInfo(goodsInfo);
				return WebServiceClient.operateCollectGoods(collectGoods, 0,
						Constants.loginResponse.getUserToken());
			} else if (type == 2) { // 添加购物车

				// 如果是需要填写

				long result = 0;
				ShopCar shopCar = new ShopCar();
				shopCar.setGoodsId(goodsInfo.getGoodsId());
				CartInfo cartInfo = new CartInfo();
				cartInfo.setGoodsId(goodsInfo.getGoodsId());
				cartInfo.setGoodsName(goodsInfo.getGoodsName());
				cartInfo.setGoodsNumber(1);
				if (goodsDetail.getDiscountPrice() > 0) {
					cartInfo.setGoodsPrice(goodsDetail.getDiscountPrice());
				} else {
					cartInfo.setGoodsPrice(goodsInfo.getShopPrice());
				}
				cartInfo.setGoodsImg(goodsInfo.getGoodsThumb());
				cartInfo.setGoodsSn(goodsInfo.getGoodsSn());
				boolean isCustomSizeSelected = false;
				if (selectedMap != null) {
					int size = 0;
					Iterator<Integer> iterator = selectedMap.keySet()
							.iterator();
					StringBuffer goodsAttr = new StringBuffer();
					while (iterator.hasNext()) {
						int arrId = iterator.next();
						if (size != 0) {
							goodsAttr.append(",");
						}
						String value = selectedMap.get(arrId).getAttrValue();
						goodsAttr.append(Constants.getWhoAttributeName(arrId)
								+ ":" + selectedMap.get(arrId).getAttrValue());
						if (value.indexOf("custom size") != -1
								|| value.indexOf("Custom size") != -1
								|| value.indexOf("custom Size") != -1
								|| value.indexOf("Custom Size") != -1) {
							isCustomSizeSelected = true;
						}
						size = size + 1;
					}
					if (isCustomSizeSelected) {
						if (goodsDetail.getCatId() == Constants.menFormalId) {

							// 检查数据是否正确填写
							String shoulder = ((EditText) baseView
									.findViewById(R.id.menShoulder_text))
									.getText().toString().trim();

							String chest = ((EditText) baseView
									.findViewById(R.id.menChest_text))
									.getText().toString().trim();

							String waist = ((EditText) baseView
									.findViewById(R.id.menWaist_text))
									.getText().toString().trim();

							String sleeve = ((EditText) baseView
									.findViewById(R.id.menSleeve_text))
									.getText().toString().trim();

							String jacketLength = ((EditText) baseView
									.findViewById(R.id.menJacketLength_text))
									.getText().toString().trim();

							String hip = ((EditText) baseView
									.findViewById(R.id.menHip_text)).getText()
									.toString().trim();

							String pantsLength = ((EditText) baseView
									.findViewById(R.id.menPantsLength_text))
									.getText().toString().trim();

							if (Constants.isStringNotEmpty(shoulder)
									&& Constants.isStringNotEmpty(chest)
									&& Constants.isStringNotEmpty(waist)
									&& Constants.isStringNotEmpty(sleeve)
									&& Constants.isStringNotEmpty(jacketLength)
									&& Constants.isStringNotEmpty(hip)
									&& Constants.isStringNotEmpty(pantsLength)) {
								goodsAttr
										.append(";Shoulder:" + shoulder + "cm");
								goodsAttr.append(",Chest:" + chest + "cm");
								goodsAttr.append(",Waist:" + waist + "cm");
								goodsAttr.append(",Sleeve:" + sleeve + "cm");
								goodsAttr.append(",Jacket Length:"
										+ jacketLength + "cm");
								goodsAttr.append(",Hip:" + hip + "cm");
								goodsAttr.append(",Pants Length:" + pantsLength
										+ "cm");
							} else {
								dialog("waring", "please fill these fields");
								return false;
							}

						} else {
							String bust = ((EditText) baseView
									.findViewById(R.id.bust_text)).getText()
									.toString().trim();

							String waist = ((EditText) baseView
									.findViewById(R.id.waist_text)).getText()
									.toString().trim();

							String hip = ((EditText) baseView
									.findViewById(R.id.hip_text)).getText()
									.toString().trim();

							String length = ((EditText) baseView
									.findViewById(R.id.lenght_text)).getText()
									.toString().trim();

							if (Constants.isStringNotEmpty(bust)
									&& Constants.isStringNotEmpty(waist)
									&& Constants.isStringNotEmpty(hip)
									&& Constants.isStringNotEmpty(length)) {
								goodsAttr.append(";Bust:" + bust + "cm");
								goodsAttr.append(",Waist:" + waist + "cm");
								goodsAttr.append(",Hip:" + hip + "cm");
								goodsAttr.append(",Length:" + length + "cm");
							} else {
								return false;
							}

						}

					}
					cartInfo.setGoodsAttr(goodsAttr.toString());
				}
				shopCar.setCartInfo(cartInfo);
				DbHelperAdapter dbHelperAdapter = new DbHelperAdapter(context);
				dbHelperAdapter.open();
				result = dbHelperAdapter.create(shopCar);
				dbHelperAdapter.close();
				if (result == 0) {
					return false;
				} else {
					return true;
				}
			}
			return false;
		}

		@Override
		protected void onPostExecute(Boolean result) {
			if (result) {
				Toast.makeText(context, "Add successfully", 500).show();
			} else {
				Toast.makeText(context, "Add failure", 500).show();
			}
			Intent myIntent = new Intent();// 创建Intent对象
			myIntent.setAction("com.wholesale.utils.UpdateUIService");
			if (type == 2) {
				myIntent.putExtra("cmd", Constants.CMD_ADDCAR_SERVICE);
			} else if (type == 1) {
				myIntent.putExtra("cmd", Constants.CMD_ADDFAVORITE_SERVICE);
			}
			myIntent.putExtra("from", "com.wholesale.GoodsDetailLayout");
			((Activity) getContext()).sendBroadcast(myIntent);
			loadingLayout.setVisibility(View.GONE);
		}

		@Override
		protected void onCancelled() {
			super.onCancelled();
		}
	}

	class GoodsGalleryAsync extends AsyncTask<Integer, Integer, GoodsGallery> {
		public GoodsGalleryAsync(Context context) {
		}

		@Override
		protected void onPreExecute() {
			progress.setVisibility(View.VISIBLE);
		}

		@Override
		protected GoodsGallery doInBackground(Integer... params) {
			GoodsGallery result = WebServiceClient.getGallery(params[0]);
			if (result != null && result.getImgUrls() != null
					&& result.getImgUrls().size() > 0) {
				result.loadImage();
				// result.loadBitmap();
			} else {
				result = null;
			}
			return result;
		}

		@Override
		protected void onPostExecute(GoodsGallery result) {
			goodsGallery = result;
			if (goodsGallery != null) {
				imageAdapter = new ImageAdapter(GoodsDetailLayout.this.context);
				imageAdapter.setImageList(goodsGallery.getImageDrawable());
				imageAdapter.notifyDataSetInvalidated();
				galleryView.setAdapter(imageAdapter);
				galleryView
						.setOnItemSelectedListener(new OnItemSelectedListener() {

							@Override
							public void onItemSelected(AdapterView<?> arg0,
									View arg1, int arg2, long arg3) {
								// 控制下面的红点
								pageControl.generatePageControl(arg2);
							}

							@Override
							public void onNothingSelected(AdapterView<?> arg0) {

							}
						});
				galleryView.postInvalidate();
				pageControl = (PageControlView) findViewById(R.id.pageControl);
				pageControl.setCount(goodsGallery.getImageDrawable().size());
				pageControl.generatePageControl(0);
				pageControl.postInvalidate();
				// galleryView.setScrollToScreenCallback(pageControl);
			}
			progress.setVisibility(View.GONE);
		}

		@Override
		protected void onCancelled() {
			super.onCancelled();
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.leftBtn:
			if (goodsGallery != null && goodsGallery.getBitmapList() != null
					&& goodsGallery.getBitmapList().size() > 0) {
				for (Bitmap bit : goodsGallery.getBitmapList()) {
					if (null != bit && !bit.isRecycled()) {
						bit.recycle();
					}
				}
			}
			viewFlipper.showPrevious();
			viewFlipper.removeView(this);
			break;
		case R.id.addCart:
			String message = "";
			String lengthErrorStr = "";
			boolean isLengthError = false;
			if (null != goodsDetail && isFormal) {
				if (goodsDetail.getCatId() == Constants.menFormalId) {
					String shoulder = ((EditText) baseView
							.findViewById(R.id.menShoulder_text)).getText()
							.toString().trim();

					String chest = ((EditText) baseView
							.findViewById(R.id.menChest_text)).getText()
							.toString().trim();

					String waist = ((EditText) baseView
							.findViewById(R.id.menWaist_text)).getText()
							.toString().trim();

					String sleeve = ((EditText) baseView
							.findViewById(R.id.menSleeve_text)).getText()
							.toString().trim();

					String jacketLength = ((EditText) baseView
							.findViewById(R.id.menJacketLength_text)).getText()
							.toString().trim();

					String hip = ((EditText) baseView
							.findViewById(R.id.menHip_text)).getText()
							.toString().trim();

					String pantsLength = ((EditText) baseView
							.findViewById(R.id.menPantsLength_text)).getText()
							.toString().trim();

					String checkValue = shoulder;
					if (checkValue == null || checkValue.equals("")) {
						message = message + "Shoulder,";
					}
					checkValue = chest;
					if (checkValue == null || checkValue.equals("")) {
						message = message + "Chest,";
					}
					checkValue = waist;
					if (checkValue == null || checkValue.equals("")) {
						message = message + "Waist,";
					}
					checkValue = sleeve;
					if (checkValue == null || checkValue.equals("")) {
						message = message + "Sleeve,";
					}
					checkValue = jacketLength;
					if (checkValue == null || checkValue.equals("")) {
						message = message + "Jacket Length,";
					}
					checkValue = hip;
					if (checkValue == null || checkValue.equals("")) {
						message = message + "Hip,";
					}
					checkValue = pantsLength;
					if (checkValue == null || checkValue.equals("")) {
						message = message + "Pants Length,";
					}
				} else {

					// TODO lc 需要判断礼服的长度属性
					int formalLengthType = 0;
					if (goodsDetail.getCatId() == 216
							|| goodsDetail.getCatId() == 196) {
						for (WhoGoodsAttr attr : whoGoodsAttrList) {
							if (attr.getAttrValue().equalsIgnoreCase(
									"weddingFormal")) {
								formalLengthType = Integer.parseInt(attr
										.getAttrPrice().trim());
							}
						}
					}
					String checkValue = ((EditText) baseView
							.findViewById(R.id.bust_text)).getText().toString();
					if (checkValue == null || checkValue.equals("")) {
						message = message + "Bust,";
					}
					checkValue = ((EditText) baseView
							.findViewById(R.id.hip_text)).getText().toString();
					if (checkValue == null || checkValue.equals("")) {
						message = message + "Hip,";
					}
					checkValue = ((EditText) baseView
							.findViewById(R.id.lenght_text)).getText()
							.toString();
					double length = Double.parseDouble(checkValue);
					if (checkValue == null || checkValue.equals("")) {
						message = message + "Lenght,";
					}
					checkValue = ((EditText) baseView
							.findViewById(R.id.waist_text)).getText()
							.toString();
					if (checkValue == null || checkValue.equals("")) {
						message = message + "Waist,";
					}

					switch (formalLengthType) {
					case 1:
						if (!(length >= 60 && length <= 90)) {
							isLengthError = true;
							lengthErrorStr = "Length should between 60 and 90";
						}
						break;
					case 2:
						if (!(length >= 91 && length <= 110)) {
							isLengthError = true;
							lengthErrorStr = "Length should between 91 and 110";
						}
						break;
					case 3:
						if (!(length >= 105 && length <= 180)) {
							isLengthError = true;
							lengthErrorStr = "Length should between 105 and 180";
						}
						break;
					}

				}
			}
			if (!message.equals("")) {
				dialog("Error", "please enter the " + message);
			} else if (isLengthError) {
				dialog("Error", lengthErrorStr);
				// 设置焦点
				((EditText) baseView.findViewById(R.id.lenght_text))
						.requestFocus();
			} else {
				AddGoodsAsync addCartAsync = new AddGoodsAsync(context);
				addCartAsync.execute(2);
			}
			break;
		case R.id.addFavorites:
			if (null != goodsDetail && Constants.userInfo != null) {
				AddGoodsAsync addGoodsAsync = new AddGoodsAsync(context);
				addGoodsAsync.execute(1);
			} else {
				UserLoginLayout userLoginLayout = new UserLoginLayout(context,
						viewFlipper);
				viewFlipper.addView(userLoginLayout);
				viewFlipper.showNext();
			}
			break;
		case R.id.goodsDescLayout:
			if (goodsDetail != null) {
				GoodsDescLayout goodsDescLayout = new GoodsDescLayout(context,
						viewFlipper, goodsDetail.getGoodsDesc());
				viewFlipper.addView(goodsDescLayout);
				viewFlipper.showNext();
			}
			break;
		case R.id.goodsCommentLayout:
			if (null != goodsDetail) {
				GoodsCommentsLayout goodsCommentsLayout = new GoodsCommentsLayout(
						context, viewFlipper, goodsInfo.getGoodsId());
				viewFlipper.addView(goodsCommentsLayout);
				viewFlipper.showNext();
			}
			break;
		default:
			break;
		}
	}

	private class ImageAdapter extends BaseAdapter {
		private Context context;
		List<String> imgUrls = new ArrayList<String>();
		List<Drawable> imageList = new ArrayList<Drawable>();

		public ImageAdapter(Context context) {
			this.context = context;
		}

		public void setImageUrls(List<String> imgUrls) {
			this.imgUrls = imgUrls;
		}

		public void setImageList(List<Drawable> imageList) {
			this.imageList = imageList;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.widget.Adapter#getCount()
		 */
		@Override
		public int getCount() {
			return imageList.size();
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.widget.Adapter#getItem(int)
		 */
		@Override
		public Object getItem(int position) {
			return null;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.widget.Adapter#getItemId(int)
		 */
		@Override
		public long getItemId(int position) {
			return 0;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.widget.Adapter#getView(int, android.view.View,
		 * android.view.ViewGroup)
		 */
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			final ImageView imageView = new ImageView(context);
			imageView.setImageDrawable(imageList.get(position));
			imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
			imageView.setMaxWidth(imageView.getMeasuredHeight());
			imageView.setLayoutParams(new Gallery.LayoutParams(140, 160));
			return imageView;
		}

	}

	class SelectClickListener implements OnClickListener {
		private Context context;
		private String title = "Select";
		private String[] names;
		private RadioOnClick radioOnClick;

		private boolean isCustom;

		public SelectClickListener(Context context, boolean isCustom) {
			this.context = context;
			this.isCustom = isCustom;
		}

		@Override
		public void onClick(View v) {
			if (selectMap.get(v.getTag()) != null) {
				if (radioOnClick == null) {
					radioOnClick = new RadioOnClick(0, v, isCustom);
				}
				ArrayList<WhoGoodsAttr> list = selectMap.get(v.getTag());
				title = Constants.getWhoAttributeName(Integer.parseInt(v
						.getTag().toString()));
				int size = list.size();
				names = new String[size];
				for (int i = 0; i < size; i++) {
					names[i] = list.get(i).getAttrValue();
				}
				AlertDialog ad = new AlertDialog.Builder(context)
						.setTitle(title)
						.setSingleChoiceItems(names, radioOnClick.getIndex(),
								radioOnClick).create();
				radioListView = ad.getListView();
				ad.show();
			}
		}
	}

	public void dialog(String title, final String message) {
		AlertDialog.Builder builder = new Builder(context);
		builder.setTitle(title);
		builder.setMessage(message);
		builder.setPositiveButton("Confirm",
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
		builder.create().show();
	}

	/**
	 * 点击单选框事件
	 * 
	 */
	class RadioOnClick implements DialogInterface.OnClickListener {
		private int index;
		private View view;
		boolean isCustom;

		public void onClick(DialogInterface dialog, int which) {
			selectedMap.put(Integer.parseInt(view.getTag().toString()),
					selectMap.get(view.getTag()).get(which));
			String value = selectedMap.get(view.getTag()).getAttrValue();
			if (isCustom) {
				if (isFormal) {
					// Formal 判断男性还是女性
					if (goodsDetail.getCatId() == Constants.menFormalId) {
						customSizeLayoutMen.setVisibility(View.VISIBLE);
					} else {
						customSizeLayout.setVisibility(View.VISIBLE);
					}
				} else {
					customSizeLayout.setVisibility(View.GONE);
					customSizeLayoutMen.setVisibility(View.GONE);
				}
			}
			((Button) view).setText(value);
			setIndex(which);
			dialog.dismiss();
		}

		public void setView(View view) {
			this.view = view;
		}

		public RadioOnClick(int index, View view, boolean isCustom) {
			this.index = index;
			this.view = view;
			this.isCustom = isCustom;
		}

		public void setIndex(int index) {
			this.index = index;
		}

		public int getIndex() {
			return index;
		}
	}
	// @Override
	// public boolean onTouch(View v, MotionEvent event) {
	//
	// return flingView.setOnTouchEvent(event);
	// }
}
